open(BUFF,"/data1/bsi/BORA_processing/devel/eqtl/outfile");
open(BUFF1,"/data1/bsi/BORA_processing/devel/eqtl/outfile_expr");
$line = <BUFF>;
chomp($line);
$line =~ s/^\s+//;
@geno = split(" ",$line);
#print WRBUFF $line."\n";
$line = <BUFF1>;
chomp($line);
$line =~ s/^\s+//;
@expr = split("\t",$line);
#print @geno."\t".@expr."\n";
for($i=0;$i<@geno;$i++)
{
	#print $geno[$i]."\n";
	$g{$geno[$i]} =1;
}
$k =0;
for($i=0;$i<@expr;$i++)
{
	#print $expr[$i]."\n";
	if(exists($g{$expr[$i]}))
	{
        	$h{$expr[$i]} =$k;
		$k++;
	}
}
#print $k."\n";
for($i=0;$i<@geno;$i++)
{
	if(exists($h{$geno[$i]}))
	{
		$jj[$i] = $h{$geno[$i]}+1;
		#print $jj[$i]."\n";
		$jj2[$h{$geno[$i]}] = $geno[$i];
	}
	else
	{
		$jj[$i] = -1;
	}
}


for($i=0;$i<@expr;$i++)
{
	if(exists($h{$expr[$i]}))
	{
		$jj1[$i] = $h{$expr[$i]}+1;	
		#print $jj1[$i]."\n";
		$jj3[$h{$expr[$i]}] = $expr[$i];
	}
	else
	{
		$jj1[$i] =-1;
	}
}
$line_geno = join(" ",@jj2);
#print $line_geno."\n";
$line_expr = join("\t",@jj3);
#print $line_expr."\n";
open(WRB,">/data1/bsi/BORA_processing/devel/eqtl/outfile_result");
print WRB "GeneName\tSNPName\tIntercept_Estimate\tIntercept_Std.Error\tIntercept_tvalue\tIntercept_Pr(>|t|)\tgeno1_Estimate\tgeno1_Std. Error\tgeno1_tvalue\tgeno1_Pr(>|t|)\n"; 
#$line = <BUFF>;
#$line = <BUFF>;
#print @jj."\t".@jj1."\n";
#$kk =0;
while($line = <BUFF>)
{
#	if($kk > 1)
#	{
#		die;
#	}
#	$kk++;
	open(WRBUFF,">/data1/bsi/BORA_processing/devel/eqtl/temp/genotype.txt");
	open(WRBUFF1,">/data1/bsi/BORA_processing/devel/eqtl/temp/expr.txt");
	print WRBUFF $line_geno."\n";
	print WRBUFF1 $line_expr."\n";
	chomp($line);
	@array = split("\t",$line);
	for($i=1;$i<@array;$i++)
	{
		@gg = split(/ /,$array[$i]);
		$gg1 = shift(@gg);
		#print $gg1[0]."\n";a
		#print @gg."\t".@jj."\n";
		for($k=0;$k<@gg;$k++)
		{
			if($jj[$k] != -1 )
			{
				$gg1[$jj[$k]] = $gg[$k];
			} 
			if($jj[$k] == 0)
			{
				die "$jj[$k] $k\n";
			}
		}
		#print $gg1[0]."\n";
		$line = join(" ",@gg1);
		$line = $gg1.$line;
		print WRBUFF $line."\n";
	}
	@array1 = ();
	#$line = <BUFF1>;
	$line = <BUFF1>;
	chomp($line);
	@ee = split("\t",$line);
	$ee1 = shift(@ee);
	for($k=0;$k<@ee;$k++)
        {
        	if($jj1[$k] != -1)
               	{
                	$ee1[$jj1[$k]] = $ee[$k];
                }
        }
        $line = join("\t",@ee1);
	$line  = $ee1.$line;
	print WRBUFF1 $line."\n";

	system("/usr/local/biotools/r/R-2.12.2/bin/Rscript /data1/bsi/BORA_processing/devel/eqtl/file.R");
	open BU,"/data1/bsi/BORA_processing/devel/eqtl/temp/gg" or die "gene:$ee1";
	open BU1,"/data1/bsi/BORA_processing/devel/eqtl/temp/gg1" or die "gene:$ee1";
	$genenames = <BU1>;
	chomp($genenames);
	$snpnames = <BU1>;
	chomp($snpnames);
	@genenames = ();
	@snpnames =();
	@genenames = split(/\t/,$genenames);
	@snpnames = split(/\t/,$snpnames);
	$ky = 0;
	@array =();
	@finalarray = ();
	while($line=<BU>)
	{
		chomp($line);
		
		@array = split(/\t/,$line);
		$finalarray[$ky] = $array[1];
		$ky++;
	}
	$ky = 0;
	for($ii=0;$ii<@finalarray;$ii++)
	{
		#$t1 = sprintf("%.2f",$finalarray[$ii]);
		$t1 = $finalarray[$ii];
		$ii++;
		#$t2 = sprintf("%.2f",$finalarray[$ii]);
		$t2 = $finalarray[$ii];
                $ii++;
		#$t3 = sprintf("%.2f",$finalarray[$ii]);
		$t3 = $finalarray[$ii];
                $ii++;
		#$t4 = sprintf("%.2f",$finalarray[$ii]);
		$t4 = $finalarray[$ii];
                $ii++;
		#$t5 = sprintf("%.2f",$finalarray[$ii]);
		$t5 = $finalarray[$ii];
                $ii++;
		#$t6 = sprintf("%.2f",$finalarray[$ii]);
		$t6 = $finalarray[$ii];
                $ii++;
		$t7 = $finalarray[$ii];
		#$t7 = sprintf("%.2f",$finalarray[$ii]);
                $ii++;
		#$t8 = sprintf("%.2f",$finalarray[$ii]);
		$t8 = $finalarray[$ii];
		print WRB $genenames[$ky]."\t".$snpnames[$ky]."\t".$t1."\t".$t3."\t".$t5."\t".$t7."\t".$t2."\t".$t4."\t".$t6."\t".$t8."\n";
		$ky++;
	}
	#print WRB $line."\n";
	system("rm /data1/bsi/BORA_processing/devel/eqtl/temp/*");
}
